package com.hdoit.apollo.web.stock.apis;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.common.enums.inventory.EndType;
import com.hdoit.apollo.logical.common.enums.inventory.InventoryType;
import com.hdoit.apollo.logical.inventory.model.Inventory;
import com.hdoit.apollo.logical.inventory.model.InventoryShow;
import com.hdoit.apollo.logical.inventory.service.IInventoryService;
import com.hdoit.apollo.logical.viewmodel.stock.InventorySearch;
import com.tennetcn.authority.component.webapi.BaseApi;
import com.tennetcn.authority.component.webapi.ResponseModel;
import com.tennetcn.authority.component.webapi.ResponseStatus;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.core.common.utils.JsonUtils;
import com.tennetcn.data.enums.ModelStatus;
import com.tennetcn.data.message.PagerModel;

@Controller
@RequestMapping(value = "/api/v1/inventory/",method = { RequestMethod.GET,RequestMethod.POST},produces = "application/json;charset=UTF-8")
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(value="/api/v1/inventory")
public class InventoryApi extends BaseApi{

	@Autowired
	private IInventoryService inventoryService;
	
	private final String saveInventoryApi="saveInventory";
	private final String getInventorysByScrollApi="getInventorysByScroll";
	
    @RequestMapping(value = saveInventoryApi)
    @ApiOperation(notes = saveInventoryApi, httpMethod = "POST", value = "保存盘点信息信息")
    public @ResponseBody String saveInventory(Inventory inventory,String inventoryShowsJson){
    	Inventory inventoryPan = inventoryService.queryModel(inventory.getInventoryId());
    	if(inventoryPan==null){
    		inventory.setInventoryNo("PD-"+ DateUtils.convertDate2String("yyyyMMddHHmmss",DateUtils.getCurrent()));
    		inventory.setCreateDate(DateUtils.getCurrent());
    		inventory.setCreateUserId(getLoginId());
    		inventory.setCreateUserName(getLoginName());
            inventory.setDeleteMark(YesOrNoInteger.no);
            inventory.setModelStatus(ModelStatus.add);
    	}else{
    		inventory.setModifyUserId(getLoginId());
    		inventory.setModifyUserName(getLoginName());
    		inventory.setModifyDate(DateUtils.getCurrent());
    		inventory.setModelStatus(ModelStatus.update);
    	}
    	
    	ResponseModel rm=new ResponseModel();
    	if(EndType.adjustment.equals(inventory.getEndType())){
        	@SuppressWarnings("unchecked")
    		List<InventoryShow> inventoryShows = (List<InventoryShow>) JsonUtils.readValues(inventoryShowsJson, List.class, InventoryShow.class);
        	if(InventoryType.inventory.equals(inventory.getInventoryType())) {
            	if(!inventoryService.applyInventory(inventory,inventoryShows)){
            		rm.setStatus(ResponseStatus.dataError);
            	}
        	}else {
        		if(!inventoryService.applyInventoryShow(inventory,inventoryShows)){
            		rm.setStatus(ResponseStatus.dataError);
            	}
			}

    	}else {
        	if(!inventoryService.applyChange(inventory)){
        		rm.setStatus(ResponseStatus.dataError);
        	}
    	}

    	return rm.toJson();
    }
    
 	@RequestMapping(value = getInventorysByScrollApi)
    @ApiOperation(notes = getInventorysByScrollApi, httpMethod = "POST", value = "滚动查找盘点信息列表")
    public @ResponseBody String getInventorysByScrollApi(InventorySearch search,int dataPosition,int pageSize){
    	ResponseModel rm=new ResponseModel();
    	PagerModel pagerModel=null;
    	if(pageSize!=0){
    		int pageindex = (dataPosition%pageSize==0?dataPosition/pageSize:dataPosition/pageSize+1)+1;
    		pagerModel = new PagerModel(pageSize, pageindex);
    	}else{
    		pagerModel=new PagerModel();
    	}

    	List<Inventory> inventorys=inventoryService.queryListMP(search, pagerModel);
    	rm.put("inventorys",inventorys);
    	return rm.toJson();
    }

}
